define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'assistant/index',
                    add_url: 'assistant/add',
                    edit_url: 'assistant/edit',
                    del_url: 'assistant/del',
                    multi_url: 'assistant/multi',
                    send_url: 'assistant/send',
                    table: 'assistant',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'admin_ids', title: __('Admin_ids')},
                        // {field: 'chatid', title: __('Chatid')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                          buttons: [
                            {name: 'run', text: '发送', title: '发送', icon: 'fa fa-rocket fa-fw', classname: 'btn  btn-xs btn-success btn-change btn-run-assistant'},
                          ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
          //切换显示隐藏变量字典列表
          $(document).on("click", "input[name='row[msgtype]']", function (e) {
            $(".text,.textcard,.markdown").remove();

            if($(this).val() == 'text')
            {
              var text = '<div class="form-group text"><label for="desc" class="control-label col-xs-12 col-sm-2">描述:</label><div class="col-xs-12 col-sm-8"><textarea class="form-control" id="desc" name="row[text][content]"></textarea></div></div>';
              $('.safe').after(text);
            }
            if($(this).val() == 'textcard')
            {
              var textcard = '<div class="form-group textcard">\n' +
                '        <label for="desc" class="control-label col-xs-12 col-sm-2">标题:</label>\n' +
                '        <div class="col-xs-12 col-sm-8">\n' +
                '            <input class="form-control"  data-rule="required" type="text" id="title" name="row[textcard][title]" placeholder="标题，不超过128个字节，超过会自动截断">\n' +
                '        </div>\n' +
                '    </div>\n' +
                '    <div class="form-group textcard">\n' +
                '        <label for="desc" class="control-label col-xs-12 col-sm-2">URL:</label>\n' +
                '        <div class="col-xs-12 col-sm-8">\n' +
                '            <input class="form-control"  data-rule="required" type="text" id="url" name="row[textcard][url]" placeholder="请填写正确的url" >\n' +
                '        </div>\n' +
                '    </div>\n' +
                '    <div class="form-group  textcard ">\n' +
                '        <label for="desc" class="control-label col-xs-12 col-sm-2">描述:</label>\n' +
                '        <div class="col-xs-12 col-sm-8 " >\n' +
                '            <textarea class="form-control" data-rule="required"  id="description" name="row[textcard][description]" placeholder="描述，不超过512个字节，超过会自动截断" ></textarea>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '    <div class="form-group  textcard ">\n' +
                '        <label for="desc" class="control-label col-xs-12 col-sm-2">特殊说明:</label>\n' +
                '        <div class="col-xs-12 col-sm-8 " >\n' +
                '            <span>卡片消息的展现形式非常灵活，支持使用br标签或者空格来进行换行处理，也支持使用div标签来使用不同的字体颜色，目前内置了3种文字颜色：灰色(gray)、高亮(highlight)、默认黑色(normal)，将其作为div标签的class属性即可。</span>\n' +
                '        </div>\n' +
                '    </div>';
              $('.safe').after(textcard);
            }

            if($(this).val() == 'markdown')
            {
              var markdown = '<div class="form-group  markdown ">\n' +
                '        <label for="desc" class="control-label col-xs-12 col-sm-2">描述:</label>\n' +
                '        <div class="col-xs-12 col-sm-8">\n' +
                '            <textarea class="form-control editor"  data-rule="required" rows="5"  id="markdown" name="row[markdown][content]" placeholder="内容，最长不超过2048个字节，必须是utf8编码" ></textarea>\n' +
                '        </div>\n' +
                '    </div><br><a target="_blank" class="control-label col-xs-12 col-sm-2 markdown" href="https://work.weixin.qq.com/api/doc#90000/90135/90236/%E6%94%AF%E6%8C%81%E7%9A%84markdown%E8%AF%AD%E6%B3%95">语法说明</a>';
              $('.safe').after(markdown);
            }
            Form.api.bindevent($("form[role=form]"));
          });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});